package arg

import (
	"errors"
	"fmt"
	_ "github.com/denisenkom/go-mssqldb"
	_ "github.com/go-sql-driver/mysql"
	_ "github.com/godror/godror"
	"github.com/gurkankaymak/hocon"
	"db2s/conn"
	"db2s/global"
	"reflect"
	"strings"
)

type dSnsLevel struct {
	SrcDSN string `json:"srcDSN"`
	DstDSN string `json:"dstDSN"`
}

func (rc *ConfigParameter) jdbcSpl(d, j string) map[string]any {
	var (
		m  = make(map[string]any)
		dd string
	)
	switch d {
	case "srcDSN":
		dd = "src"
	case "dstDSN":
		dd = "dst"
	}
	m[d] = j
	if strings.Contains(j, "|") {
		f := strings.Split(j, "|")
		f1 := f[0]
		for k, v := range []string{"dbname", "drive"} {
			if k == 1 {
				m[fmt.Sprintf("%v_%v", dd, v)] = driveTo(f1)
			} else {
				m[fmt.Sprintf("%v_%v", dd, v)] = f1
			}
		}
		m[fmt.Sprintf("%v_%v", dd, "jdbc")] = f[1]
	}
	return m
}

// dSNsSecondaryLevelParameterCheck
func dSNsSecondaryLevelParameter(dSns hocon.Object) (DSns DSNsS, err error) {
	var (
		dSnsMap map[string]any
	)
	if dSnsMap, err = global.StructToMapAny(getDSN()); err != nil {
		return
	}
	for k, v := range dSnsMap {
		if f1 := dSns.ToConfig().Get(k); f1 == nil {
			err = errors.New(fmt.Sprintf("%v connection jdbc does not exist. Please configure!!!", k))
			return
		}
		s2 := rc.getHoconConfigVal(dSns.ToConfig(), k, reflect.TypeOf(v).String()).(string)
		if strings.HasPrefix(s2, "\"") && strings.HasSuffix(s2, "\"") {
			s2 = s2[1 : len(s2)-1]
		}
		if err = global.MapToStructJson(rc.jdbcSpl(k, s2), &DSns); err != nil {
			return
		}
	}
	DSns.SourceConnText = conn.ConText{Name: DSns.SrcDBName, Device: DSns.SrcDrive, Jdbc: DSns.SrcJdbc}
	DSns.TargetConnText = conn.ConText{Name: DSns.DestDBName, Device: DSns.DestDrive, Jdbc: DSns.DestJdbc}
	return
}

type dnsCheck struct {
	Jdbc   string `json:"jdbc"`
	DbName string `json:"dbname"`
	Drive  string `json:"drive"`
}
type dSnsModeCheck struct {
	FunctionalMode    []string       `json:"funcMode"`
	FuncPassParameter map[string]any `json:"funcPass"`
}

// checkDSnsParameter
func checkDSnsParameter(p *SecondaryLevel, s dSnsModeCheck) (err error) {
	var (
		dSnsModeParameter map[string]any
		mode              = p.FunctionalMode
	)
	if dSnsModeParameter, err = global.StructToMapAny(s); err != nil {
		return err
	}
	if v, ok := dSnsModeParameter["funcMode"]; ok {
		var funcExist bool
		for _, v1 := range v.([]string) {
			if strings.EqualFold(mode, v1) {
				funcExist = true
			}
		}
		if !funcExist {
			return nil
		}
	}
	if _, ok := dSnsModeParameter["funcPass"]; !ok {
		return
	}
	if _, ok := dSnsModeParameter["funcPass"].(map[string]any)[mode]; !ok {
		return
	}
	v1 := dSnsModeParameter["funcPass"].(map[string]any)[mode]
	for _, v2 := range v1.(map[string]dnsCheck) {
		fmt.Println("-------111:", v2)
		//var ll conn.ConEr
		//if ll, err = conn.NewConn(v2.DbName); err != nil || ll == nil {
		//	return err
		//}
		//if err = ll.Set("conText", conn.ConText{Name: v2.DbName, Device: v2.Drive, Jdbc: v2.Jdbc}); err != nil {
		//	return err
		//}
		//if err = ll.Open(); err != nil {
		//	fmt.Println("-------err:", v2.Drive, err)
		//}
		//if err = ll.Close(); err != nil {
		//	return err
		//}
	}
	return
}
